package Leetcode.DayTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName Test_424
 * @since: 2024/11/17 10:28
 * @auth: kirito
 * @description: 周赛424
 **/
public class Test_424 {
    public static void main(String[] args) {
        Test_424 test = new Test_424();
        int[] nums = {0};
        int[][] queries ={{0, 0, 2}, {0, 0, 4}, {0, 0, 4}, {0, 0, 3}, {0, 0, 5}};
        System.out.println(isZeroArray(nums, queries));
    }
/*    public int countValidSelections(int[] nums) {
        int count = 0;
        // 找到所有可能的起始位置
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                // 模拟向左移动
                if (simulate(nums.clone(), i, -1)) {
                    count++;
                }
                // 模拟向右移动
                if (simulate(nums.clone(), i, 1)) {
                    count++;
                }
            }
        }
        return count;
    }

    private boolean simulate(int[] nums, int curr, int direction) {
        while (curr >= 0 && curr < nums.length) {
            if (nums[curr] == 0) {
                // 如果当前值为0，继续沿当前方向移动
                curr += direction;
            } else {
                // 如果当前值不为0，减1并反转方向
                nums[curr]--;
                direction = -direction;
                curr += direction;
            }
        }
        // 检查是否所有元素都变为0
        for (int num : nums) {
            if (num != 0) {
                return false;
            }
        }
        return true;
    }*/

    public int countValidSelections(int[] nums) {
        int n = nums.length;
        int[] leftSum = new int[n];
        int[] rightSum = new int[n];
        leftSum[0] = nums[0];
        rightSum[n - 1] = nums[n - 1];
        for (int i = 1; i < n; i++) {
            leftSum[i] = leftSum[i - 1] + nums[i];
        }

        for (int i = n - 2; i >= 0; i--) {
            rightSum[i] = rightSum[i + 1] + nums[i];
        }


        int ans = 0;
        for (int i = 0; i < n ; i++) {
            if (nums[i] == 0) {
                if (leftSum[i] == rightSum[i]) {
                    ans += 2;
                }
                if (leftSum[i] == rightSum[i] - 1) {
                    ans += 1;
                }
                if (leftSum[i] == rightSum[i] + 1) {
                    ans += 1;
                }

            }

        }
        return ans;
    }
    public static boolean isZeroArray(int[] nums, int[][] queries) {
        //差分yyds！！！
        int n = nums.length;
        int[] diff = new int[n + 1];
        for (int[] query : queries) {
            int li = query[0];
            int ri = query[1];
            diff[li] -= 1;
            diff[ri + 1] += 1;
        }

        int cur = 0;
        for (int i = 0; i < n; i++) {
            cur += diff[i];
            if (nums[i] + cur > 0) {
                return false;
            }
        }
        return true;
    }

    public int minZeroArray(int[] nums, int[][] queries) {
        int n = nums.length;
        int[] diff = new int[n + 1];
        int k = 0;
        boolean flag = true;
        for (int num : nums) {
            if (num != 0) {
                flag = false;
                break;
            }
        }
        if (flag) {
            return k;
        }
        for (int[] query : queries) {
            k++;
            int li = query[0];
            int ri = query[1];
            int val = query[2];
            diff[li] -= val;
            diff[ri + 1] += val;
            int cur = 0;
            flag = true;
            for (int i = 0; i < n; i++) {
                cur += diff[i];
                if (nums[i] + cur > 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return k;
            }
        }
        return -1;
    }


}
